<?php

namespace App\Models;


class AdminOld extends BaseUser
{

    // 以前深蓝汇的权限


    // 用户管理
    protected $table = 'admin';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id'];

    // 获取所有的
    public function getAll($where = [], $order = 'id ASC')
    {
        // 字段
        $fields = $this->getTableField(true);
        $fields = array_diff($fields, ['role', 'password']);
        // 查询
        $lists = $this->where($where)->select($fields)->orderByRaw($order)->get()->toArray();
        $lists = array_column($lists, null, 'id');


        return $lists;
    }

    // 权限
    public function getRole()
    {
        $roles = [
            ['role' => 'plan', 'name' => '首页',],
            [
                'role'  => 'admin',
                'name'  => '管理员管理',
                'child' => [
                    ['role' => 'video-cate', 'name' => '管理员管理',],
                    ['role' => 'user', 'name' => '用户管理',],
                    ['role' => 'news-cate', 'name' => '日志管理',],
                ],
            ],
            [
                'role'  => 'goods',
                'name'  => '商品管理',
                'child' => [
                    ['role' => 'video-cate', 'name' => '商品管理',],
                    ['role' => 'user', 'name' => '商品分类',],
                ],
            ],
            [
                'role'  => 'order',
                'name'  => '运营管理',
                'child' => [
                    ['role' => 'news-cate', 'name' => '商品评论',],
                    ['role' => 'video-cate', 'name' => '订单管理',],
                    ['role' => 'video-cate', 'name' => '用户管理',],
                ],
            ],
            [
                'role'  => 'order',
                'name'  => '站点配置',
                'child' => [
                    ['role' => 'video-cate', 'name' => '系统配置',],
                    ['role' => 'video-cate', 'name' => '单页配置',],
                ],
            ],
        ];


        return $roles;
    }

    // 检查权限
    public function checkRole($roles, $controller, $param)
    {
        // 基础：只有控制器的权限判断
        if(in_array($controller, $roles)){
            return true;
        }

        // 参数
        // 控制器与栏目id组合的权限 => news.1
        // 特殊权限 => news.is_header
        if(in_array($controller.'.'.$param, $roles)){
            return true;
        }

        return false;
    }

    // 整理组合拥有的权限
    // 是否需要展示父级（用于查看的时候不需要展示父级）
    public function makeRoles($roles, $is_parent = true)
    {
        $all    = $this->getRole();
        $result = $this->showTree($all, $roles, $is_parent);


        return $result;
    }

    // 生成树状
    public function toTree($data = [], $channel = 'news', $myid = 0, $limitLevel = 0, $currentLevel = 0)
    {
        $trees = [];
        $data  = array_values($data);
        foreach($data as $key => $val){
            // 不是这个分类的 不能使用
            if($val['channel'] != $channel){
                continue;
            }

            if($val['pid'] == $myid){
                if($limitLevel > 0 && $limitLevel == $currentLevel){
                    return $trees;
                }

                $item = [];
                // 组合
                $item['role']    = $channel.'.'.$val['id'];
                $item['cate_id'] = $val['id'];
                $item['id']      = $val['id'];
                $item['pid']     = $val['pid'];
                $item['name']    = $val['name'];
                $item['channel'] = $channel;

                unset($data[$key]);

                $children = $this->toTree($data, $channel, $val['id'], $limitLevel, ($currentLevel + 1));
                if(!empty($children)){
                    //$val['child']  = $children;
                    $item['child'] = $children;
                }
                $trees[] = $item;
            }
        }

        return $trees;
    }

    // 获取树状 展示
    public function showTree($tree, $roles, $is_parent = true)
    {
        $result = [];
        foreach($tree as $item){
            if(in_array($item['role'], $roles)){
                $result[] = $item;
                continue;
            }
            // 如果有子级
            if(!empty($item['child'])){
                $result2 = $this->showTree($item['child'], $roles, $is_parent);
                // 如果选中子级栏目
                if($result2){
                    if($is_parent){
                        // 展示父级
                        $result[] = ['role' => $item['role'], 'name' => $item['name'], 'channel' => $item['channel'] ?? '', 'child' => $result2];
                    } else{
                        // 不展示父级
                        $result = empty($result) ? $result2 : array_merge($result, $result2);
                    }
                }
            }
        }

        return $result;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
            'username' => 'required',
            'salt'     => 'required',
        ];

        $edit = [
        ];

        $info = [
            'password' => ['max:129'],
            'is_del'   => ['in:0,1,2'],
            //'role'     => ['in:0,1,2'],
        ];

        $rule = [
            //'nickname' => 'required',
            //'role'       => ['required'],
            //'article_no' =>['numeric'],
            //'mobile'     =>['required'],
            'password' => ['max:65'],
            'role'     => ['in:0,1,2'],
            'status'   => ['in:0,1,2'],
        ];


        $msg = [
            'id'         => '编号',
            'nickname'   => '姓名',
            'username'   => '用户名',
            'mobile'     => '手机号',
            'password'   => '密码',
            'role'       => '角色',
            'article_no' => '创建文章数',
            'salt'       => '随机码',
            'status'     => '状态',
            'ctime'      => '创建时间',
            'mtime'      => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add, 'info' => $info];
    }

}
